<?php

namespace app\admin\controller;

use app\common\model\mysql\AdminUser;
use think\facade\View;

class Login extends AdminBase
{
    public function initialize()
    {
        if ($this->isLogin()) {
            return $this->redirect(url('index/index'), 302);
        }
    }

    /**
     * 登录静态页面
     * @return string
     * @throws \Exception
     */
    public function index() {
        //echo md5('adminzihou');
//        print_r(session(config('admin.session_admin')));
        return View::fetch();
    }

    public function check() {
        $captcha = $this->request->param('captcha', '', 'trim');
        $username = $this->request->param('username', '', 'trim');
        $password = $this->request->param('password', '', 'trim');
        $data = [
            'username' => $username,
            'password'=> $password,
            'captcha'=> $captcha
        ];
        $validate = new \app\admin\validate\AdminUser();
        if (!$validate->check($data)) {
            return show(config('status.error'), $validate->getError());
        }
        //验证用户名/密码是否匹配
        try {
            (new \app\admin\business\AdminUser())->login($data);
        } catch (\Exception $e) {
            return show(config('status.error'), $e->getMessage());
        }
        return show(config('status.success'), '登录成功');
    }
}